// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/dwio/common/file_sink.h>

#include <gtest/gtest.h>

namespace kumo::pollux::dwio::common {

class MemorySinkTest : public testing::Test {
 protected:
  static void SetUpTestCase() {
    memory::MemoryManager::testingSetInstance({});
  }

  std::shared_ptr<pollux::memory::MemoryPool> pool_{
      memory::memoryManager()->addLeafPool()};
};

TEST_F(MemorySinkTest, create) {
  std::string chars("abcdefghijklmnopqrst");
  std::vector<DataBuffer<char>> buffers;

  // Add 'abcdefghij' to first buffer
  buffers.emplace_back(*pool_);
  buffers.back().append(0, chars.data(), 10);

  // Add 'klmnopqrst' to second buffer
  buffers.emplace_back(*pool_);
  buffers.back().append(0, chars.data() + 10, 10);

  ASSERT_EQ(buffers.size(), 2);

  auto memorySink = std::make_unique<MemorySink>(
      1024, dwio::common::FileSink::Options{.pool = pool_.get()});

  ASSERT_TRUE(memorySink->isBuffered());
  // Write data to MemorySink.
  memorySink->write(buffers);
  ASSERT_EQ(memorySink->size(), chars.length());
  ASSERT_EQ(memorySink->data(), chars);
  memorySink->close();
}
} // namespace kumo::pollux::dwio::common
